package mwa.cs.hh.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Document;

import mwa.cs.dao.ArDAO;
import mwa.cs.dao.CommentDAO;
import mwa.cs.dao.CustHHDAO;
import mwa.cs.dao.CustMonthlyDAO;
import mwa.cs.dao.SMErrorLogDAO;
import mwa.cs.dao.ZoneDAO;
import mwa.cs.database.Database;
import mwa.cs.hh.biz.MeterFunction;
import mwa.cs.model.Ar;
import mwa.cs.model.CustHH;
import mwa.cs.model.CustMonthly;
import mwa.cs.model.Customer;
import mwa.cs.util.JDate;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class CS0304000Srvl
 */
public class CS34000Srvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int vnSiteId;
	private int vnUserId;
	private String vaUrlAddress = "";
	private String vaAgentBrowser = "";
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CS34000Srvl() {
		super();
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		vnUserId = Integer.parseInt(request.getSession().getAttribute("user_id").toString());vaUrlAddress = request.getParameter("url_address");
		vaAgentBrowser = request.getParameter("agent_browser");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/JSON");
		String vaProcessType = request.getParameter("methodType");
		if(vaProcessType.equals("getComment")){
			getCommnet(request, response);
		}else if (vaProcessType.equals("getDataAR")){
			getDataAR(request, response);
		} else if (vaProcessType.equals("getVatRate")) {
			getVatRate(request, response);
		} else if (vaProcessType.equals("calConsumption")) {
			calConsumption(request, response);
		} else {
			saveData(request, response);
		}
	}
	
	@SuppressWarnings("unchecked")
	public void getCommnet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		
		Database db = new Database();
		PrintWriter voOut = response.getWriter();
		String[] vaMessage = null, vaValue = null;
		
		CommentDAO voComment = new CommentDAO(db);
		List<Map<String, Object>> voResult = voComment.findComment(vnSiteId);
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);
			vaValue[i] = (String) voObject.get("COMMENT_CODE");
			vaMessage[i] = (String) voObject.get("COMMENT_DESCRIPTION");
		}
		
		db.close();
		JSONObject voObject = new JSONObject();
		JSONArray voListMessages = new JSONArray();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i++) {
				voListMessages.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voListMessages);
		JSONArray voListValues = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i++) {
				voListValues.add(vaValue[i]);
			}
		}
		voObject.put("values", voListValues);
		voOut.print(voObject.toJSONString());
	}
	
	@SuppressWarnings("unchecked")
	public void saveData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		Database db = new Database();
		CustHH voCustHH = new CustHH();
		Ar voAR = new Ar();
		CustMonthly voCustMonthly = new CustMonthly();
    	ArDAO voARDAO = new ArDAO(db);
    	CustHHDAO voCustHHDAO = new CustHHDAO(db);
    	CustMonthlyDAO voCustMonthlyDAO = new CustMonthlyDAO(db);
    	vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());

		String vaAccount = request.getParameter("account");
		String vaDateAmount = "";
		double vnAverageConsumptionNew = 0.00;
		String vaPeriodMount = voCustHHDAO.findMaxMonth(vnSiteId);
		String vaPeriodYear = voCustHHDAO.findMaxYear(vnSiteId);
		String vaConsumption = request.getParameter("consumption").replace(",", "").trim();
		String vaPreviousReadDate = request.getParameter("previousReadDate");
		String vaCurrentReadDate = request.getParameter("currentReadDate");
		String vaPreviousReadNumber = request.getParameter("previousReadNumber").replace(",", "").trim();
		String vaCurrentReadNumber = request.getParameter("currentReadNumber").replace(",", "").trim();
		String vaWaterCharge = request.getParameter("waterCharge").replace(",", "").trim();
		String vaWaterFee = request.getParameter("waterFee").replace(",", "").trim();
		String vaVatAmount = request.getParameter("vatAmount").replace(",", "").trim();
		String vaBillAmount = request.getParameter("billAmount").replace(",", "").trim();
		String vaGarbageCharge = request.getParameter("garbageCharge").replace(",", "").trim();
		String vaWasteWaterCharge = request.getParameter("wasteWaterCharge").replace(",", "").trim();
		String vaOtherCharge = request.getParameter("otherCharge").replace(",", "").trim();
		String vaTotalAmount = request.getParameter("totalAmount").replace(",", "").trim();
		String vaComment = request.getParameter("comment");
		int vaUpdateUserId = 1;
		String vaAverageConsumptionNew = "";
		// เช็คค่า ถ้าเป็นค่า ว่าง ให้เป็น 0
		if (vaGarbageCharge.equals("")) vaGarbageCharge = "0.00";
		double vnGarbageCharge = Double.parseDouble(vaGarbageCharge);
		if (vaWasteWaterCharge.equals("")) vaWasteWaterCharge = "0.00";
		double vnWasteWaterCharge = Double.parseDouble(vaWasteWaterCharge);
		if (vaOtherCharge.equals("")) vaOtherCharge = "0.00";
		double vnOtherCharge = Double.parseDouble(vaOtherCharge);
		try{
			
			vaDateAmount = MeterFunction.CalDurationBetweenTwoDates(vaPreviousReadDate, vaCurrentReadDate);
			vnAverageConsumptionNew = Double.parseDouble(vaConsumption) / Double.parseDouble(vaDateAmount);
			vaAverageConsumptionNew = String.format("%.2f", vnAverageConsumptionNew);
		} catch(ParseException e){
			SMErrorLogDAO.getInstance().addLog(0, "CS34000", JDate.getDate(), JDate.getTime(),vnUserId, "CS34000Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);	
			e.printStackTrace();
		}
		
		// set ค่า CustHH
		voCustHH.setVnAveragevnConsumptionNew(Double.parseDouble(vaAverageConsumptionNew));
		voCustHH.setVnNewConsumption(Integer.parseInt(vaConsumption));
		voCustHH.setVaPreviousReadDate(vaPreviousReadDate.substring(6, 10) + vaPreviousReadDate.substring(3, 5) + vaPreviousReadDate.substring(0, 2));
		voCustHH.setVnPreviousReadNumber(Integer.parseInt(vaPreviousReadNumber));
		voCustHH.setVaCurrentReadDate(vaCurrentReadDate.substring(6, 10) + vaCurrentReadDate.substring(3, 5) + vaCurrentReadDate.substring(0, 2));
		voCustHH.setVnCurrentReadNumber(Integer.parseInt(vaCurrentReadNumber));
		voCustHH.setVaCommentCode(vaComment);
		voCustHH.setVnWaterCharge(Double.parseDouble(vaWaterCharge));
		voCustHH.setVnWaterFee(Double.parseDouble(vaWaterFee));
		voCustHH.setVnVatAmount(Double.parseDouble(vaVatAmount));
		voCustHH.setVnBillAmount(Double.parseDouble(vaBillAmount));
		voCustHH.setVaUploadDate(JDate.getDate());
		voCustHH.setVaUploadTime(JDate.getTime());
		voCustHH.setVnWasteWater_charge(vnWasteWaterCharge);
		voCustHH.setVnGrabageCharge(vnGarbageCharge);
		voCustHH.setVnOtherCharge(vnOtherCharge);
		voCustHH.setVnNumberfDay(Integer.parseInt(vaDateAmount));
		voCustHH.setVnUpdateUserId(vaUpdateUserId);
		voCustHH.setVaUpdateDate(JDate.getDate());
		voCustHH.setVaUpdateTime(JDate.getTime());
		voCustHH.setVnSiteId(vnSiteId);
		voCustHH.setVaAccount(vaAccount);
		voCustHH.setVaPeriodMonth(vaPeriodMount);
		voCustHH.setVaPeriodYear(vaPeriodYear);
		
		// set ค่า  AR
		voAR.setVnConsumption(Integer.parseInt(vaConsumption));
		voAR.setVnWaterFee(Double.parseDouble(vaWaterFee));
		voAR.setVnWaterCharge(Double.parseDouble(vaWaterCharge));
		voAR.setVnVatAmount(Double.parseDouble(vaVatAmount));
		voAR.setVnBillAmount(Double.parseDouble(vaBillAmount));
		voAR.setVaPreviousReadDate(vaPreviousReadDate.substring(6, 10) + vaPreviousReadDate.substring(3, 5) + vaPreviousReadDate.substring(0, 2));
		voAR.setVnPreviousReadNumber(Integer.parseInt(vaPreviousReadNumber));
		voAR.setVaCurrentReadDate(vaCurrentReadDate.substring(6, 10) + vaCurrentReadDate.substring(3, 5) + vaCurrentReadDate.substring(0, 2));
		voAR.setVnCurrentReadNumber(Integer.parseInt(vaCurrentReadNumber));
		voAR.setVnTotalAmount(Double.parseDouble(vaTotalAmount));
		voAR.setVnWasteWaterCharge(Double.parseDouble(vaWasteWaterCharge));
		voAR.setVnGarbageCharge(Double.parseDouble(vaGarbageCharge));
		voAR.setVnOtherCharge(Double.parseDouble(vaOtherCharge));
		voAR.setVnUpdateUserId(vaUpdateUserId);
		voAR.setVaUpdateDate(JDate.getDate());
		voAR.setVaUpdateTime(JDate.getTime());
		voAR.setVnSiteId(vnSiteId);
		voAR.setVaAccount(vaAccount);
		voAR.setVaPeriodMonth(vaPeriodMount);
		voAR.setVaPeriodYear(vaPeriodYear);
		
		// set ค่า CustMonthly
		voCustMonthly.setVaMeterReadNumber(vaCurrentReadNumber);
		voCustMonthly.setVaReadDate(vaCurrentReadDate.substring(6, 10) + vaCurrentReadDate.substring(3, 5) + vaCurrentReadDate.substring(0, 2));
		voCustMonthly.setVnConsumption(Integer.parseInt(vaConsumption));
		voCustMonthly.setVnAverageConsumption(Double.parseDouble(vaAverageConsumptionNew));
		voCustMonthly.setVnWaterFee(Double.parseDouble(vaWaterFee));
		voCustMonthly.setVnWaterCharge(Double.parseDouble(vaWaterCharge));
		voCustMonthly.setVnVatAmount(Double.parseDouble(vaVatAmount));
		voCustMonthly.setVnBillAmount(Double.parseDouble(vaBillAmount));
		voCustMonthly.setVaCommentCode(vaComment);
		voCustMonthly.setVnUpdateUserId(vaUpdateUserId);
		voCustMonthly.setVaUpdateDate(JDate.getDate());
		voCustMonthly.setVaUpdateTime(JDate.getTime());
		voCustMonthly.setVnSiteId(vnSiteId);
		voCustMonthly.setVaAccount(vaAccount);
		voCustMonthly.setVaPeriodMonth(vaPeriodMount);
		voCustMonthly.setVaPeriodYear(vaPeriodYear);
		
		try{
			db.beginTransaction();
			boolean vbResult = false;
			vbResult = voCustHHDAO.updateCustHH(voCustHH);
			if (vbResult) {
				vbResult = voARDAO.updateMeterAR(voAR);
				if (vbResult){
					vbResult = voCustMonthlyDAO.updateMeterCustMonthly(voCustMonthly);
					if (vbResult) {
						voObject.put("message", "success");
					} else {
						voObject.put("message", "fail");
					}
				} else {
					voObject.put("message", "fail");
				}
			} else {
				voObject.put("message", "fail");
			}
			db.commit();
		} catch (Exception e) {
			SMErrorLogDAO.getInstance().addLog(0, "CS34000", JDate.getDate(), JDate.getTime(),vnUserId, "CS34000Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);	
			db.rollback();
			voObject.put("message", "fail");
		}
		db.close();
		voOut.print(voObject.toJSONString());
	}
	
	@SuppressWarnings("unchecked")
	public void getDataAR(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		Database db = new Database();
    	ArDAO voARDAO = new ArDAO(db);
    	CustHHDAO voCustHHDAO = new CustHHDAO(db);
    	CustMonthlyDAO voCustMonthlyDAO = new CustMonthlyDAO(db);
    	vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());

		String vaAccount = request.getParameter("account");
		String vaPeriodMount = voCustHHDAO.findMaxMonth(vnSiteId);
		String vaPeriodYear = voCustHHDAO.findMaxYear(vnSiteId);
		JSONArray voBillNumber = new JSONArray();
		JSONArray voZoneName = new JSONArray();
		JSONArray voRouteDescription = new JSONArray();
		JSONArray voSequence = new JSONArray();
		JSONArray voAccount = new JSONArray();
		JSONArray voFullName = new JSONArray();
		JSONArray voCustomerClass = new JSONArray();
		JSONArray voCustomerClassId = new JSONArray();
		JSONArray voAddress = new JSONArray(); 
		JSONArray voAllowance = new JSONArray();
		JSONArray voPreviousReadDate = new JSONArray();
		JSONArray voCurrentReadDate = new JSONArray();
		JSONArray voPreviousReadNumber = new JSONArray();
		JSONArray voCurrentReadNumber = new JSONArray();
		JSONArray voConsumption = new JSONArray();
		JSONArray voWaterCharge = new JSONArray();
		JSONArray voWaterFee = new JSONArray();
		JSONArray voVatAmount = new JSONArray();
		JSONArray voBillAmount = new JSONArray();
		JSONArray voGarbageCharge = new JSONArray();
		JSONArray voWasteWaterCharge = new JSONArray();
		JSONArray voOtherCharge = new JSONArray();
		JSONArray voTotalAmount = new JSONArray();
		JSONArray voStatus = new JSONArray();
		JSONArray voReceiveDate = new JSONArray();
		JSONArray voPrintBillNumber = new JSONArray();
		
		List<Map<String, Object>> voArrayList = null;
		Map<String, Object> voMap = new HashMap<String, Object>();
		voArrayList = voARDAO.searchMeterAccount(vnSiteId, vaAccount, vaPeriodMount, vaPeriodYear);
    	if (voArrayList.size() == 0){
    		voObject.put("message", "resultNull");
    	} else {
    		if (voArrayList.size() > 1){
    			voObject.put("message", "resultNull");
    		} else {
    			for (int i = 0; i < voArrayList.size(); i++){
        			voMap = voArrayList.get(i);
        			voStatus.add(voMap.get("STATUS"));
        			voReceiveDate.add(voMap.get("RECEIVE_DATE"));
        			voPrintBillNumber.add(voMap.get("PRINT_BILL_NUMBER"));
        			if (voStatus.get(i).equals("0")) {
        				voObject.put("message", "status");
        			} else if (voReceiveDate.get(i) != null && voReceiveDate.get(i) != "") {
        				voObject.put("message", "receiveDate");
        			} else if (voPrintBillNumber.get(i).equals("1")) {
        				voObject.put("message", "printBillNumber");
        			} else {
        				String commentCode = voCustMonthlyDAO.getCommentCode(vnSiteId, vaAccount, vaPeriodMount, vaPeriodYear);
        				voBillNumber.add(voMap.get("BILL_NUMBER"));
            			voZoneName.add(voMap.get("ZONE_NAME"));
            			voRouteDescription.add(voMap.get("ROUTE_DESCRIPTION"));
            			voSequence.add(voMap.get("SEQ"));
            			voAccount.add(voMap.get("ACCOUNT"));
                		voFullName.add(voMap.get("FULL_NAME"));
                		voCustomerClass.add(voMap.get("DESCRIPTION"));
                		voCustomerClassId.add(voMap.get("CUSTOMER_CLASS"));
                		voAddress.add(voMap.get("ADDRESS"));
                		voAllowance.add(voMap.get("ALLOWANCE"));
                		voPreviousReadDate.add(voMap.get("PREVIOUS_READ_DATE"));
                		voCurrentReadDate.add(voMap.get("CURRENT_READ_DATE"));
                		voPreviousReadNumber.add(voMap.get("PREVIOUS_READ_NUMBER"));
                		voCurrentReadNumber.add(voMap.get("CURRENT_READ_NUMBER"));
                		voConsumption.add(voMap.get("CONSUMPTION"));
                		voWaterCharge.add(voMap.get("WATER_CHARGE"));
                		voWaterFee.add(voMap.get("WATER_FEE"));
                		voVatAmount.add(voMap.get("VAT_AMOUNT"));
                		voBillAmount.add(voMap.get("BILL_AMOUNT"));
                		voGarbageCharge.add(voMap.get("GARBAGE_CHARGE"));
                		voWasteWaterCharge.add(voMap.get("WASTEWATER_CHARGE"));
                		voOtherCharge.add(voMap.get("OTHER_CHARGE"));
                		voTotalAmount.add(voMap.get("TOTAL_AMOUNT"));
                		
                		voObject.put("billNumber", voBillNumber);
                		voObject.put("zoneName", voZoneName);
                		voObject.put("routeDescription", voRouteDescription);
                		voObject.put("sequence", voSequence);
                		voObject.put("account", voAccount);
                		voObject.put("fullName", voFullName);
                		voObject.put("customerClass", voCustomerClass);
                		voObject.put("customerClassId", voCustomerClassId);
                		voObject.put("address", voAddress);
                		voObject.put("allowance", voAllowance);
                		voObject.put("commentCode", commentCode);
                		voObject.put("previousReadDate", voPreviousReadDate);
                		voObject.put("currentReadDate", voCurrentReadDate);
                		voObject.put("previousReadNumber", voPreviousReadNumber);
                		voObject.put("currentReadNumber", voCurrentReadNumber);
                		voObject.put("consumption", voConsumption);
                		voObject.put("waterCharge", voWaterCharge);
                		voObject.put("waterFee", voWaterFee);
                		voObject.put("vatAmount", voVatAmount);
                		voObject.put("billAmount", voBillAmount);
                		voObject.put("garbageCharge", voGarbageCharge);
                		voObject.put("wasteWaterCharge", voWasteWaterCharge);
                		voObject.put("otherCharge", voOtherCharge);
                		voObject.put("totalAmount", voTotalAmount);
        			}
        			
        		}
    		}
    	}
    	db.close();
    	voOut.print(voObject.toJSONString());
	}
	
	@SuppressWarnings("unchecked")
	public void getVatRate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());

		String vaWaterCharge = request.getParameter("waterCharge");
		String vaWaterFee = request.getParameter("waterFee");
		String vaCurrentDate = JDate.getDate();
		String vaVatAmountNew = MeterFunction.calVatRate(vnSiteId, vaCurrentDate, vaWaterCharge, vaWaterFee);
		voObject.put("vatAmountNew", vaVatAmountNew);
		voOut.print(voObject.toJSONString());
	}
	
	@SuppressWarnings("unchecked")
	public void calConsumption(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());

		double vnWaterChargeNew = 0.00;
		String vaDateAmount = "";
		String vaWaterChargeLast = "";
		String vaTypeAllowance = "";
		double vnDiscount = 0.00;
		DecimalFormat voFormatter = new DecimalFormat("#,###.00");
		String vaAccount = request.getParameter("account");
		double vnConsumption = Double.parseDouble(request.getParameter("consumption"));
		String vaTypeCal = request.getParameter("typeCal");
		String vaCurrentReadDate = request.getParameter("currentReadDate");
		String vaPreviousReadDate = request.getParameter("previousReadDate");
		String vaCustomerClass = request.getParameter("customerClass");
		String vaAllowance = request.getParameter("allowance");
		String [] vaData = MeterFunction.getDiscountAllowance(vnSiteId);
		vaTypeAllowance = vaData[0];
		vnDiscount = Double.parseDouble(vaData[1]);
		// ประเภทของการคำนวณ
		if (vaTypeCal.equals("G")) { // คำนวณแบบปกติ
			// ส่วนลดทหารผ่านศึก
			if (vaAllowance.equals("1")) { 
				double percentConsumption = 0.00;
				if (vaTypeAllowance.equals("C")) { // กรณีลด % หน่วยน้ำที่ใช้
					percentConsumption = vnConsumption * vnDiscount;
					vnConsumption = vnConsumption - percentConsumption;
					vnWaterChargeNew = Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, vnConsumption).replace(",", ""));
				} else { // กรณีลด ค่าน้ำ
					vnWaterChargeNew = Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, vnConsumption).replace(",", "")) - vnDiscount;
				}
			} else { // ถ้าไม่มีส่วนลดทหารผ่านศึก
				vnWaterChargeNew = Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, vnConsumption).replace(",", ""));
			}
		} else { // คำนวณแบบเฉลี่ย
			try{
				double waterUnit = 0.00;
				vaDateAmount = MeterFunction.CalDurationBetweenTwoDates(vaPreviousReadDate, vaCurrentReadDate);
				vaWaterChargeLast = MeterFunction.calculateAverageConsumption(vnSiteId, vaAccount);
				// ส่วนลดทหารผ่านศึก
				if (vaAllowance.equals("1")) {
					if (vaTypeAllowance.equals("C")) { // กรณีลด % หน่วยน้ำที่ใช้
						double percentConsumption = 0.00;
						waterUnit = Double.parseDouble(vaDateAmount) * Double.parseDouble(vaWaterChargeLast);
						percentConsumption = waterUnit * vnDiscount;
						vnConsumption = waterUnit - percentConsumption;
						vnWaterChargeNew = Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, vnConsumption).replace(",", ""));
					} else { // กรณีลด ค่าน้ำ
						waterUnit = Double.parseDouble(vaDateAmount) * Double.parseDouble(vaWaterChargeLast);
						vnConsumption = waterUnit;
						vnWaterChargeNew = (Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, vnConsumption).replace(",", ""))) - vnDiscount;
					}
				} else { // ถ้าไม่มีส่วนลดทหารผ่านศึก
					waterUnit = (Double.parseDouble(vaDateAmount) * Double.parseDouble(vaWaterChargeLast));
					vnConsumption = waterUnit;
					vnWaterChargeNew = Double.parseDouble(MeterFunction.calNormalUsedUnitWater(vnSiteId, vaCustomerClass, waterUnit).replace(",", ""));
				}
			}
			catch(ParseException e){
				SMErrorLogDAO.getInstance().addLog(0, "CS34000", JDate.getDate(), JDate.getTime(),vnUserId, "CS34000Srvl", e.getMessage(),vaUrlAddress, vaAgentBrowser);	
				e.printStackTrace();
			}
		}
		voObject.put("consumption", (int) Math.round(vnConsumption));
		voObject.put("waterChargeNew", voFormatter.format(vnWaterChargeNew));
		voOut.print(voObject.toJSONString());
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
